capture log close
clear all
set more off
pause off
# delimit;

local output "";
local log "";
local data "";

/* 
Create Figure A3.2A
*/

use "`data'\collapse_combine.dta", clear;


/* ******************************* */
/* **** DATA PREP AND RESHAPE **** */
/* ******************************* */

/* Age */
gen age=year-ser_dobyy;
replace age=99 if year==9999;
order ser_dobyy year age;

/* Create <=1937 and >=1943 cohorts */
tab ser_dobyy, m;
gen ser_dobcoh=1937 if ser_dobyy>=1935 & ser_dobyy<=1937;
replace ser_dobcoh=1943 if ser_dobyy>=1943 & ser_dobyy<=1945;
replace ser_dobcoh=9999 if ser_dobcoh==.;
order ser_dobyy ser_dobcoh year age;


/* Sum over month-age and YOB cohort */
drop 	
	earn10yr_male earn10yr_race_w earn10yr_race_h earn10yr_race_b earn10yr_race_a earn10yr_race_o earn10yr_earnyrnum60 earn10yr_earnavg25to55
	earn55_male earn55_race_w earn55_race_h earn55_race_b earn55_race_a earn55_race_o earn55_earnyrnum60 earn55_earnavg25to55;
collapse (sum) earn10yr_* earn55_*, by(age ser_dobcoh);
sort ser_dobcoh age;

/* Create fractions of cohorts instead of counts (for flow variable, use single event denominator) */
foreach set in earn10yr earn55 {;
foreach stub in `set'_lfexit_flow `set'_lfexit_one `set'_lfexit_one_1 `set'_lfexit_one_rbst `set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany `set'_earndelt55to60 {;
	egen total_`stub'=total(`stub'), by(ser_dobcoh);
};
};

foreach set in earn10yr earn55 {;
	foreach stub in 
		`set'_lfexit_one `set'_lfexit_one_1 `set'_lfexit_one_rbst 
		`set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany `set'_earndelt55to60 
		{;	
		rename `stub' ct_`stub';
		gen `stub'=ct_`stub'/total_`stub' if ct_`stub'>10;
		replace `stub'=. if ct_`stub'<=10;
	};
		rename `set'_lfexit_flow ct_`set'_lfexit_flow;
		gen `set'_lfexit_flow=ct_`set'_lfexit_flow/total_`set'_lfexit_one;
		replace `set'_lfexit_flow=. if ct_`set'_lfexit_flow<=10;
};
drop total_* ct_*;

/* Create hazard variables based on fractions  */
drop if ser_dobcoh == 9999 ;
sort ser_dobcoh age ;
summ age ;
local max = r(max) ;
local min = r(min) ;
foreach set in earn10yr earn55 { ;
	by ser_dobcoh: gen `set'_lfexit_one_cum = sum(`set'_lfexit_one) ;
	gen `set'_hzlast_alt = `set'_lfexit_one/(1 - `set'_lfexit_one_cum[_n-1]) ;
	summ `set'_hzlast_alt `set'_hzlast_frac ;
	drop `set'_lfexit_one_cum ;
} ;

/* Reshape for plot */
drop if age<50;
reshape wide 
	earn10yr_lfexit_flow earn10yr_lfexit_one earn10yr_lfexit_one_1 earn10yr_lfexit_one_rbst 
	earn10yr_claim_phusold earn10yr_claim_phusany earn10yr_claim_mbaold earn10yr_claim_mbaany earn10yr_earndelt55to60
	earn10yr_lfexitflow_frac earn10yr_hzfirst_frac earn10yr_hzlast_frac earn10yr_hzlast_alt earn10yr_restat_num_rflow
	earn55_lfexit_flow earn55_lfexit_one earn55_lfexit_one_1 earn55_lfexit_one_rbst
	earn55_claim_phusold earn55_claim_phusany earn55_claim_mbaold earn55_claim_mbaany earn55_earndelt55to60
	earn55_lfexitflow_frac earn55_hzfirst_frac earn55_hzlast_frac earn55_hzlast_alt earn55_restat_num_rflow
, i(age) j(ser_dobcoh);


/* ***************************************************** */
/* **** PLOT RETIREMENT AND CLAIMING ACROSS COHORTS **** */
/* ***************************************************** */

local color_control "blue";
local color_treatment "red";

/* Retirement */
keep if age>=55 & age<69 ;

local cond_lfexit_one		"if age>=55 & age<69" ;

foreach set in earn10yr {;
foreach stub in lfexit_one {;
twoway 	
	connected `set'_`stub'1937 age if age>=55 & age<69, lcolor(`color_control') lwidth(medthick) lpattern(solid) mcolor(`color_control') msymbol(sh) ||
	connected `set'_`stub'1943 age `cond_`stub'', lcolor(`color_treatment') lwidth(medthick) lpattern(solid) mcolor(`color_treatment') msymbol(o)
	xtitle("Age", size(medsmall)) xsc(r(55 68)) xlab(55(1)68)
	ytitle("Fraction of birth cohort", size(medsmall))
	legend(col(2) row(1) label(1 "Pre-Reform") label(2 "Post-Reform"))
	graphregion(color(white))
	bgcolor(white)
	ylabel(,nogrid)
	;
	graph export "`output'\\Figure_A32A.png", replace;
};
};


